﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Visifire.Charts;

namespace DIST.LR.ZHJG.Controls.Chart
{
    public partial class ucVFDoughnut : UserControl
    {
        public ucVFDoughnut()
        {
            InitializeComponent();
            this.DoughnutChart.Loaded += new RoutedEventHandler(DoughnutChart_Loaded);
            #region 测试
            List<DoughnutValue> dd = new List<DoughnutValue>();
            dd.Add(new DoughnutValue("耕地量", 80, DoughnutChartLabelStyle.Percentage));
            dd.Add(new DoughnutValue("林地量", 20, DoughnutChartLabelStyle.Null));
            DoughnutSeriseSetting seting = new DoughnutSeriseSetting(270);
            DoughnutChartSetting titleSet = new DoughnutChartSetting("用地量百分比");
            //DoughnutChartSetting titleSet = new DoughnutChartSetting(null);
            DoughnutInfo<DoughnutValue> cc = new DoughnutInfo<DoughnutValue>();
            cc.DataSource = dd;
            cc.DoughnutSetting = seting;
            cc.DoughnutChartSet = titleSet;
            BinDingDoughnut(cc);
            #endregion

        }

        void DoughnutChart_Loaded(object sender, RoutedEventArgs e)
        {
            var c = sender as Visifire.Charts.Chart;
            IEnumerable<TextBlock> lablels = VisualTreeAPI.FindVisualChildren<TextBlock>(c.Parent as System.Windows.Controls.Grid);
            foreach (TextBlock txt in lablels)
            {
                if (txt.Text.ToLower().Contains("trial"))
                {
                    txt.Visibility = System.Windows.Visibility.Collapsed;
                    if (txt.Parent is Border)
                    {
                        (txt.Parent as Border).Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
                    }
                }
            }
        }

        public void BinDingDoughnut<T>(DoughnutInfo<T> doughnutinfo) 
        {
            #region Chart初始化设置
            Visifire.Charts.Title title = new Visifire.Charts.Title();
            title.Text = doughnutinfo.DoughnutChartSet.ChartTitle1;
            DoughnutChart.Titles.Add(title);
            Visifire.Charts.DataSeries dataSerise = new Visifire.Charts.DataSeries();
            dataSerise.RenderAs = Visifire.Charts.RenderAs.Doughnut;
            dataSerise.StartAngle = doughnutinfo.DoughnutSetting.StartAngle1;
            #endregion

            DataPoint dataPoint;
            foreach (DoughnutValue info in doughnutinfo.DataSource)
            {
                dataPoint = new DataPoint();
                dataPoint.AxisXLabel = info.AxisXLabelName1;
                dataPoint.YValue = info.YValue1;
                dataPoint.Style=this.Resources[info.LabelStyle1.ToString()] as Style;
                dataSerise.DataPoints.Add(dataPoint);
            }
            DoughnutChart.Series.Add(dataSerise);
        }
    }

    public class DoughnutInfo<T>
    {

        public DoughnutInfo()
        {
            this.DataSource = new List<DoughnutValue>();
        }

        public DoughnutSeriseSetting DoughnutSetting { get; set; }
        public List<DoughnutValue> DataSource { get; set; }
        public DoughnutChartSetting DoughnutChartSet { get; set; }
    }

    #region Chart相关设置
    /// <summary>
    /// 图表设置
    /// </summary>
    public class DoughnutChartSetting
    {
        private string ChartTitle;

        public DoughnutChartSetting(string _chartTitle)
        {
            this.ChartTitle = _chartTitle;
        }

        public string ChartTitle1
        {
            get { return ChartTitle; }
            set
            {
                if (value != null)
                {
                    ChartTitle = value;
                }
                else
                {
                    ChartTitle = null;
                }
            }
        }
    }

    /// <summary>
    /// 图表样式设置
    /// </summary>
    public class DoughnutSeriseSetting
    {
        private double StartAngle;

        public double StartAngle1
        {
            get { return StartAngle; }
            set { StartAngle = value; }
        }

        public DoughnutSeriseSetting(double _startAngle)
        {
            this.StartAngle = _startAngle;
        }
    }

    /// <summary>
    /// 枚举，定义图表文字显示样式组合
    /// </summary>
    public enum DoughnutChartLabelStyle
    {
        AxisXLabelPercentage,//文字 百分比
        AxisXLabelYValue,
        Percentage,//百分比
        XValue,//X轴数值
        YValue,//Y轴数值
        XValueYValue,//X轴数值 Y轴数值
        Null
    }

    /// <summary>
    /// 图表显示内容设置
    /// </summary>
    public class DoughnutValue
    {
        private string AxisXLabelName;
        private double YValue;
        private DoughnutChartLabelStyle LabelStyle;

        public DoughnutChartLabelStyle LabelStyle1
        {
            get { return LabelStyle; }
            set { LabelStyle = value; }
        }

        public string AxisXLabelName1
        {
            get { return AxisXLabelName; }
            set { AxisXLabelName = value; }
        }

        public double YValue1
        {
            get { return YValue; }
            set { YValue = value; }
        }

        public DoughnutValue(string _lName, double _yValue)
        {
            this.AxisXLabelName = _lName;
            this.YValue = _yValue;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_lName">AxisXLabel值</param>
        /// <param name="_yValue">Y轴数值</param>
        /// <param name="_labelStyle">文字显示组合样式</param>
        public DoughnutValue(string _lName, double _yValue, DoughnutChartLabelStyle _labelStyle)
        {
            this.AxisXLabelName = _lName;
            this.YValue = _yValue;
            this.LabelStyle = _labelStyle;
        }
    }
    #endregion
}
